
package gui;

import ground.Clay;
import ground.Gravel;
import ground.GroundType;
import ground.Loam;
import ground.Peat;
import ground.Sand;
import ground.Silt;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;

import record.ProjectRecord;
import request.RequestFactory;
import request.RequestType;
import request.SaveRequest;
import system.Client;

/**
 * Gathers requisite information about the geothermal requirements.
 * 
 * @author Jacob
 * 
 */
@SuppressWarnings("serial")
public class GeothermalRequirementsWindow extends JPanel implements ActionListener
{
	/**
	 * The client through which the program will communicate with the server
	 */
	@SuppressWarnings("unused")
	private Client client;
	/**
	 * The application which displays this window
	 */
	private Application app;
	/**
	 * ???
	 */
	@SuppressWarnings("unused")
	private JFrame sectionType;
	/**
	 * The currently active project
	 */
	private ProjectRecord record;
	/**
	 * The field which the user will enter the temperature
	 */
	private JTextField temperatureField;
	/**
	 * 
	 */
	private JTextField asField;
	/**
	 * 
	 */
	private JTextField toField;
	/**
	 * The field which the user will enter the coldest day
	 */
	private JTextField coldestField;
	/**
	 * The field which the user will enter the hottest day
	 */
	private JTextField hottestField;
	/**
	 * The OK button
	 */
	private JButton doneButton;
	/**
	 * The add layer button
	 */
	private JButton addLayerButton;
	/**
	 * The remove layer button
	 */
	private JButton removeLayerButton;
	/**
	 * The panel that is on the bottom of the form
	 */
	private JPanel lowerPanel;

	/**
	 * The list of cross section panels that should be displayed on this form
	 */
	private ArrayList<CrossSectionPanel> crossSectionsPanel;
	
	private RequestFactory factory = new RequestFactory();
	
//	/**
//	 * The checkboxes that allow cross section panels to be removed
//	 */
//	private ArrayList<JCheckBox> checkBoxes = new ArrayList<JCheckBox>();

	/**
	 * Creates this window and adds it to the application
	 * 
	 * @param pRecord
	 *            the currently active project record
	 * @param pApp
	 *            the application that will display this window
	 */
	public GeothermalRequirementsWindow(ProjectRecord pRecord, Application pApp)
	{
		client = pApp.getClient();
		record = pRecord;
		Vector<GroundType> crossSections = pRecord.getGeoData().getCrossSections();
		crossSectionsPanel = new ArrayList<CrossSectionPanel>();

		JPanel upperPanel = new JPanel();
		lowerPanel = new JPanel();
		JPanel buttonPanel = new JPanel();
		JScrollPane scroller = new JScrollPane(lowerPanel);

		lowerPanel.setLayout(new BoxLayout(lowerPanel, BoxLayout.PAGE_AXIS));
		
		upperPanel.setBorder(new EtchedBorder());
		scroller.setBorder(new EtchedBorder());
		upperPanel.setLayout(new GridLayout(5, 2));
		buttonPanel.setLayout(new GridLayout(1, 3));

		JLabel temperatureLabel = new JLabel("Temperature:");
		JLabel asLabel = new JLabel("As:");
		JLabel toLabel = new JLabel("To:");
		JLabel coldestLabel = new JLabel("Coldest Day:");
		JLabel hottestLabel = new JLabel("Hottest Day:");

		doneButton = new JButton("Done");
		addLayerButton = new JButton("Add Layer");
		removeLayerButton = new JButton("Remove Layer");

		doneButton.addActionListener(this);
		addLayerButton.addActionListener(this);
		removeLayerButton.addActionListener(this);

		temperatureField = new JTextField();
		asField = new JTextField();
		toField = new JTextField();
		coldestField = new JTextField();
		hottestField = new JTextField();

		temperatureField.setText((record.getGeoData().getFieldByName("Ground Temp").getValue().toString()));
		asField.setText((record.getGeoData().getFieldByName("AS").getValue().toString()));
		toField.setText((record.getGeoData().getFieldByName("TO").getValue().toString()));
		coldestField.setText((record.getGeoData().getFieldByName("Coldest Day").getValue().toString()));
		hottestField.setText((record.getGeoData().getFieldByName("Warmest Day").getValue().toString()));

		upperPanel.add(temperatureLabel);
		upperPanel.add(temperatureField);
		upperPanel.add(asLabel);
		upperPanel.add(asField);
		upperPanel.add(toLabel);
		upperPanel.add(toField);
		upperPanel.add(coldestLabel);
		upperPanel.add(coldestField);
		upperPanel.add(hottestLabel);
		upperPanel.add(hottestField);

		buttonPanel.add(addLayerButton);
		buttonPanel.add(removeLayerButton);
		buttonPanel.add(doneButton);

		this.setLayout(new BorderLayout());
		this.add(upperPanel, BorderLayout.NORTH);
		this.add(scroller, BorderLayout.CENTER);
		this.add(buttonPanel, BorderLayout.SOUTH);

		if (crossSections != null)
		{
			for (GroundType ground : crossSections)
			{
				if (ground.toString().equals("Clay"))
				{
					CrossSectionPanel section = new CrossSectionPanel("Clay");
					section.setDepth(ground.getDepth());
					crossSectionsPanel.add(section);
//					checkBoxes.add(new JCheckBox());
				}
				else if (ground.toString().equals("Gravel"))
				{
					CrossSectionPanel section = new CrossSectionPanel("Gravel");
					section.setDepth(ground.getDepth());
					crossSectionsPanel.add(section);
//					checkBoxes.add(new JCheckBox());
				}
				else if (ground.toString().equals("Loam"))
				{
					CrossSectionPanel section = new CrossSectionPanel("Loam");
					section.setDepth(ground.getDepth());
					crossSectionsPanel.add(section);
//					checkBoxes.add(new JCheckBox());
				}
				else if (ground.toString().equals("Peat"))
				{
					CrossSectionPanel section = new CrossSectionPanel("Peat");
					section.setDepth(ground.getDepth());
					crossSectionsPanel.add(section);
//					checkBoxes.add(new JCheckBox());
				}
				else if (ground.toString().equals("Sand"))
				{
					CrossSectionPanel section = new CrossSectionPanel("Sand");
					section.setDepth(ground.getDepth());
					crossSectionsPanel.add(section);
//					checkBoxes.add(new JCheckBox());
				}
				else if (ground.toString().equals("Silt"))
				{
					CrossSectionPanel section = new CrossSectionPanel("Silt");
					section.setDepth(ground.getDepth());
					crossSectionsPanel.add(section);
//					checkBoxes.add(new JCheckBox());
				}
			}
			refreshCrossSections();
		}

		app = pApp;
		app.getContentPane().removeAll();
		app.getContentPane().add(this);
		app.setVisible(true);
	}

	/**
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent arg0)
	{
		if (arg0.getSource().equals(doneButton))
		{
			Vector<GroundType> sections = record.getGeoData().getCrossSections();
			for (int i = 0; i < sections.size(); i++)
			{
				sections.get(i).setDepth(crossSectionsPanel.get(i).getDepth());
			}

			record.getGeoData().getFieldByName("Ground Temp").setValue(Double.parseDouble(temperatureField.getText()));
			record.getGeoData().getFieldByName("AS").setValue(Double.parseDouble(asField.getText()));
			record.getGeoData().getFieldByName("TO").setValue(Double.parseDouble(toField.getText()));
			record.getGeoData().getFieldByName("Warmest Day").setValue(Double.parseDouble(hottestField.getText()));
			record.getGeoData().getFieldByName("Coldest Day").setValue(Double.parseDouble(coldestField.getText()));

			SaveRequest save = (SaveRequest)factory.getRequest(RequestType.Save);
			save.setRecord(record);
			record.setGeothermalRequirementsComplete(true);
			try
			{
				app.getClient().sendRequest(save);
			}
			catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			catch (ClassNotFoundException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			new ProjectMenuWindow(record, app);
		}
		else if (arg0.getSource().equals(addLayerButton))
		{

			CrossSectionPanel[] crossSectionsArray = new CrossSectionPanel[6];
			crossSectionsArray[0] = new CrossSectionPanel("Clay");
			crossSectionsArray[1] = new CrossSectionPanel("Silt");
			crossSectionsArray[2] = new CrossSectionPanel("Loam");
			crossSectionsArray[3] = new CrossSectionPanel("Peat");
			crossSectionsArray[4] = new CrossSectionPanel("Sand");
			crossSectionsArray[5] = new CrossSectionPanel("Gravel");

			// the following call to showInputDialog returns null if user clicks cancel
			CrossSectionPanel crossSection = (CrossSectionPanel) JOptionPane.showInputDialog(this, "Choose a soil type:",
					"Soil Types", JOptionPane.QUESTION_MESSAGE, null, crossSectionsArray, null);
			
			if(crossSection == null)
				return;
			
			crossSectionsPanel.add(crossSection);
//			checkBoxes.add(new JCheckBox());
			if (crossSection.toString().equals("Clay"))
			{
				GroundType groundType = new Clay();
				record.getGeoData().addGroundType(groundType);
			}
			else if (crossSection.toString().equals("Gravel"))
			{
				GroundType groundType = new Gravel();
				record.getGeoData().addGroundType(groundType);
			}
			else if (crossSection.toString().equals("Loam"))
			{
				GroundType groundType = new Loam();
				record.getGeoData().addGroundType(groundType);
			}
			else if (crossSection.toString().equals("Peat"))
			{
				GroundType groundType = new Peat();
				record.getGeoData().addGroundType(groundType);
			}
			else if (crossSection.toString().equals("Sand"))
			{
				GroundType groundType = new Sand();
				record.getGeoData().addGroundType(groundType);
			}
			else if (crossSection.toString().equals("Silt"))
			{
				GroundType groundType = new Silt();
				record.getGeoData().addGroundType(groundType);
			}
			refreshCrossSections();
		}
		else if (arg0.getSource().equals(removeLayerButton))
		{
			for (int i = crossSectionsPanel.size() - 1; i >= 0; i--)
			{
				if (crossSectionsPanel.get(i).isChecked())
				{
					crossSectionsPanel.remove(i);
					record.getGeoData().removeGroundType(i);
				}
			}
			refreshCrossSections();
		}

	}

	/**
	 * Updates the cross sections that are being displayed on this form
	 */
	public void refreshCrossSections()
	{
		lowerPanel.removeAll();
		//lowerPanel.setLayout(new GridLayout(crossSectionsPanel.size(), 1));
		for (int i = 0; i < crossSectionsPanel.size(); i++)
		{
//			lowerPanel.add(checkBoxes.get(i));
			lowerPanel.add(crossSectionsPanel.get(i));
			lowerPanel.add(Box.createRigidArea(new Dimension(0, 5)));
		}
		lowerPanel.add(Box.createVerticalGlue());
		lowerPanel.setVisible(true);
		lowerPanel.repaint();
		this.validate();
	}

}
